<?php

namespace addons\facrm\controller\facrm\customer;

use addons\facrm\library\BackendApi;
use addons\facrm\model\Admin;
use app\admin\model\facrm\Fields;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;


/**
 * 客户管理
 * @icon fa fa-circle-o
 */
class Index extends BackendApi
{

    protected $model = null;
    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'id,name,mobile,telephone';
    protected $childrenAdminIds = [];
    protected $noNeedRight = ['selectpage'];
    protected $addon_config = array();
    private $expire_type = 0;
    protected $modelValidate = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\facrm\Customer();
        $this->addon_config = get_addon_config('facrm');
        $this->request->filter(['strip_tags']);
    }

    /**
     * 客户列表
     * @ApiBody ("参数参考电脑版")
     */
    public function index()
    {
        $scene = model('\app\admin\model\facrm\Scene');
        $scene_list = $scene->where('types', 'customer')
            ->where(function ($query) {
                $query->where('admin_id', 0)->whereor('admin_id', $this->auth->id);
            })
            ->column('id,name');

        $filter_w = $this->getFilterWhere($scene_list);
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            $this->request->request(['custom' => $filter_w]);
            return parent::selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = $this->model->getLoseWhere($this->addon_config, $this->expire_type)
            ->where($where)
            ->where($filter_w)
            ->with([
                'createUser' => function ($user) {
                    $user->field('id,username,nickname');
                },
                'ownerUser' => function ($user) {
                    $user->field('id,username,nickname');
                },
            ])
            ->order($sort, $order)
            ->limit($offset, $limit)->fetchSql(false)
            ->select();

        if ($this->addon_config['show_contacts']!=1){
            $pattern = '/(\d{3})(\d{4})(\d{4})/i';
            $replacement = '$1****$3';
            //为了安全隐藏手机和电话
            foreach ($list as $row) {
                $row->mobile =$row->mobile?preg_replace ($pattern,$replacement,$row->mobile ):'';
                $row->telephone =$row->telephone?preg_replace ($pattern,$replacement,$row->telephone ):'';
            }
        }
        $this->success('',array( "rows" => $list,'scene'=>$scene_list));

    }

    /**
     * 处理过滤where条件
     * @ApiInternal
     * @return array
     */
    private function getFilterWhere($scene_list)
    {
        $filter = $this->request->get("filter", '');
        $filter = (array)json_decode($filter, true);
        $filter_w = [];
        $filter_w['owner_user_id'] = $this->auth->id;
        $filter['scene_id'] = isset($filter['scene_id']) ? $filter['scene_id'] : 1;
        if (isset($filter['scene_id'])) {

            if (!isset($scene_list[$filter['scene_id']])) {
                $this->error(__("您没有权限"));
            }
            switch ($filter['scene_id']) {
                case 1:
                    //全部客户
                    $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
                    $filter_w['owner_user_id'] = ['in', $this->childrenAdminIds];
                    break;
                case 2:
                    //我的客户
                    $filter_w['owner_user_id'] = $this->auth->id;
                    break;
                case 3:
                    //下属客户
                    $this->childrenAdminIds = $this->auth->getChildrenAdminIds(false);
                    $filter_w['owner_user_id'] = ['in', $this->childrenAdminIds];
                    break;
                case 13:
                    $filter_w['owner_user_id'] = $this->auth->id;
                    $filter_w['rw_user_id'] = ['<>', ''];
                    break;
                case 14:
                    $filter_w[]=['exp',Db::raw("FIND_IN_SET('{$filter_w['owner_user_id']}',rw_user_id)")];
                    unset($filter_w['owner_user_id']);
                    break;
                default://其它的还做TODO
                    $filter_w['owner_user_id'] = $this->auth->id;
            }
            if (isset($filter['expire_type'])) {
                $this->expire_type = $filter['expire_type'];
                unset($filter['expire_type']);
            }
            unset($filter['scene_id']);
            $this->request->get(['filter' => json_encode($filter)]);
        }
        return $filter_w;
    }


    /**
     * 添加客户
     * @ApiBody ("参数参考电脑版,去掉row,取里面的.记得把自定义字段也要提交过来")
     * @return mixed
     */
    public function add()
    {
        //判断是否有客户限制
        if ($this->addon_config['possess_num'] > 0 && $this->addon_config['possess_num'] <= $this->model->where('deal_status', 0)->where('owner_user_id', $this->auth->id)->count()) {
            $this->error(__("已经超过限制拥有客户数量,限制") . $this->addon_config['possess_num']);
        }
        $params = $this->request->post();
        if ($params) {
            $result = false;
            Db::startTrans();
            try {
                $params = array_merge($params, [
                    'create_user_id' => $this->auth->id,
                    'owner_user_id' => $this->auth->id,
                    'province' => ($this->request->request('province')),
                    'city' => ($this->request->request('city')),
                    'area' => ($this->request->request('area')),
                    'next_time' =>strtotime($params['next_time']),
                    'follow_time' => time(),
                    'collect_time' => time(),
                ]);
                \app\admin\model\facrm\Operatelog::setTitle(__('移动端添加客户'));
                $result = $this->model->add($params);
                Db::commit();
            } catch (ValidateException $e) {

                Db::rollback();
                $this->error($e->getMessage());
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success();
            } else {
                $this->error($this->model->getError()?__($this->model->getError()):__('添加失败'));
            }
        }
        $this->error(__('参数不能为空', ''));
    }


    /**
     * 修改客户
     * @ApiMethod (POST|GET)
     * @ApiParams(name="id", type="int", required=true, description="客户id")
     * @ApiBody ("get是获取，post是修改，修改的内容参数参考电脑版")
     */
    public function edit()
    {
        $id=$this->request->request('id','','intval');
        if (!$id){
            $this->error(__('客户不存在'));
        }
        $row = $this->model->get($id,['ownerUser'=>function($query){
            return $query->field('id,username,nickname');
        }]);
        if (!$row) {
            $this->error(__('客户不存在，或已删除'));
        }
        $auth = new \addons\facrm\library\Auth();
        if (!$auth->checkCustomerAuth($row, $this->auth)) {
            $this->error(__('您没有权限'));
        }

        if ($this->request->isPost()) {
            $params = $this->request->post();

            if ($params) {
                $params = array_merge($params, [
                    'province' => ($this->request->param('province')),
                    'city' => ($this->request->param('city')),
                    'area' => ($this->request->param('area')),
                ]);

                $result = false;
                Db::startTrans();
                try {
                    $result = $row->edit($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error($row->getError() ? __($row->getError()) : __('修改失败'));
                }
            }
            $this->error(__('参数不能为空', ''));
        }
        $this->success('',$row);

    }

    /**
     * 删除
     * @ApiMethod (POST)
     * @param string $ids
     */
    public function del()
    {

        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }

        $ids = $this->request->post("ids");
        if ($ids) {
            $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
            $list = $this->model->where('id', 'in', $ids)->where('owner_user_id', 'in', $this->childrenAdminIds)->select();

            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
					\app\admin\model\facrm\Operatelog::setTitle(__('删除'));
                    $count += $v->del();
                }
                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success(__("删除成功"));
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    /**
     * 公海客户
     * @ApiBody ("参数参考电脑版")
     */
    public function common()
    {
        $filter_w['owner_user_id'] = 0;
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();


        $list = $this->model
            ->where($where)
            ->where($filter_w)
            ->with([
                'createUser' => function ($user) {
                    $user->field('id,username,nickname');
                },
            ])
            ->order($sort, $order)
            ->limit($offset, $limit)->fetchSql(false)
            ->select();
        foreach ($list as $row) {
            //公海为了安全隐藏手机和电话
            $row->mobile = "***";
            $row->telephone = "***";
        }

        $result = array( "rows" => $list);
        $this->success('',$result);
    }

    /**
     * 放入公海
     * @ApiMethod (POST)
     * @ApiParams(name="ids", type="string", required=true, description="客户id，多个以逗号隔开")
     */
    public function discard($ids = NULL)
    {
        $ids = $this->request->post("ids");
        if (!$ids)
            $this->error(__("请选择客户"));
        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
        $list = $this->model->where('id', 'in', $ids)->where('owner_user_id','in',$this->childrenAdminIds)->select();

        if (!$list) {
            $this->error(__('No Results were found'));
        }
        $success_num = 0;//成功数量
        foreach ($list as $row) {
            $row->discard();
            $success_num++;
        }
        $this->success("成功放入公海{$success_num}条");
    }

    /**
     * 领取
     * @ApiMethod (POST)
     * @ApiParams(name="ids", type="string", required=true, description="客户id，多个以逗号隔开")
     */
    public function receive()
    {
        $ids = $this->request->post("ids");
        if (!$ids)
            $this->error(__("请选择客户"));

        $list = $this->model->where('id', 'in', $ids)->where('owner_user_id',0)->select();
        if (!$list) {
            $this->error(__('No Results were found'));
        }
        $success_num = 0;//成功领取的数量
        foreach ($list as $row) {
            //判断是否有客户限制
            if ($this->addon_config['possess_num'] > 0 && $this->addon_config['possess_num'] <= $this->model->where('deal_status', 0)->where('owner_user_id', $this->auth->id)->count()) {
                $this->error(__("已经超过限制拥有客户数量{$this->addon_config['possess_num']}条。成功领取{$success_num}条"));
            }
            $row->owner_user_id = $this->auth->id;
            $row->follow_time = $row->collect_time = time();
            \app\admin\model\facrm\Operatelog::setTitle(__('领取'));
            $row->save();
            //联系人负责人也更改
            $contacts = model('\app\admin\model\facrm\customer\Contacts');
            $contacts->where('customer_id', $row->id)->update(['owner_user_id' => $this->auth->id]);
            $success_num++;
        }
        $this->success("成功领取{$success_num}条");
    }


    /**
     * 转移客户
     * @ApiParams(name="ids", type="string", required=true, description="客户id，多个以逗号隔开")
     * @ApiParams(name="admin_id", type="int", required=true, description="admin_id员工id")
     */
    public function divert($ids = NULL)
    {
		$ids = $this->request->request("ids");
        if (!$ids)
            $this->error(__("请选择客户"));
        if ($this->request->isPost()){
            $admin_id = $this->request->post("admin_id", '', 'intval');
            if (!$admin_id)
                $this->error(__("未选择员工"));

            $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
            $list = $this->model->where('id', 'in', $ids)->where('owner_user_id', 'in', $this->childrenAdminIds)->select();
            //判断转移的员工是不是下属
            if (!in_array($admin_id, $this->childrenAdminIds)) {
                $this->error(__('只能转移给自己或者下属'));
            }

            if (!$list) {
                $this->error(__('No Results were found'));
            }

            $owner_count = $this->model->where('deal_status', 0)->where('owner_user_id', $admin_id)->count();
            $contacts = model('\app\admin\model\facrm\customer\Contacts');
            $success_num = 0;//成功数量
            foreach ($list as $row) {
                if ($row->deal_status != 1) {//成交不占用数量
                    //判断是否有客户限制
                    if ($this->addon_config['possess_num'] > 0 && $this->addon_config['possess_num'] <= $owner_count) {
                        $this->error(__("已经超过限制拥有客户数量,限制") . $this->addon_config['possess_num'] . ",成功转移{$success_num}条!");
                    }
                    $owner_count++;
                }
               
                $row->owner_user_id = $admin_id;
				$row->collect_time = time();
                $row->save();
                //更改联系人
                //联系人负责人也更改
                $contacts->where('customer_id', $row->id)->update(['owner_user_id' => $admin_id]);

                $success_num++;


            }
            $this->success(__("成功转移{$success_num}条"));
        }

        $this->error(__("请求有误"));

    }

    /**
     * 合同列表
     */
    public function contractlist()
    {
        $customer_id = $this->request->request("customer_id");
        if (!$customer_id)
            $this->error(__("参数错误"));
        //检查是否有权限
        $Auth = new  \addons\facrm\library\Auth();
        if (!$Auth->checkCustomerAuth($customer_id, $this->auth)) {
            $this->error(__("您没有权限查看合同"));
        }
        $this->searchFields = "id,number,name,remark";
        $this->model = model('\app\admin\model\facrm\Contract');
        $filter_w['customer_id'] = $customer_id;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            $this->request->request(['custom' => $filter_w]);
            return parent::selectpage();
        }

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->where($filter_w)
            ->with([
                'createUser' => function ($user) {
                    $user->field('id,username,nickname');
                },
                'orderAdmin' => function ($user) {
                    $user->field('id,username,nickname');
                },
                'customer' => function ($customer) {
                    $customer->field('id,name');
                },
            ])
            ->order($sort, $order)
            ->limit($offset, $limit)->fetchSql(false)
            ->select();
        $result = array("rows" => $list);
        return $this->success('',$result);
    }

    /**
     * 回款列表
     */
    public function receivableslist()
    {
        $customer_id = $this->request->request("customer_id");
        if (!$customer_id)
            $this->error(__("参数错误"));
        //检查是否有权限
        $Auth = new  \addons\facrm\library\Auth();
        if (!$Auth->checkCustomerAuth($customer_id, $this->auth)) {
            $this->error(__("您没有权限查看回款"));
        }
        $this->searchFields = "id,number,remark";
        $this->model = model('\app\admin\model\facrm\contract\Receivables');
        $filter_w['customer_id'] = $customer_id;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        //如果发送的来源是Selectpage，则转发到Selectpage
        if ($this->request->request('keyField')) {
            $this->request->request(['custom' => $filter_w]);
            return parent::selectpage();
        }

        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        $list = $this->model
            ->where($where)
            ->where($filter_w)
            ->with([
                'createUser' => function ($user) {
                    $user->field('id,username,nickname');
                },
                'customer' => function ($customer) {
                    $customer->field('id,name');
                },
                'contract' => function ($customer) {
                    $customer->field('id,name,number');
                },
            ])
            ->order($sort, $order)
            ->limit($offset, $limit)->fetchSql(false)
            ->select();
        $result = array("rows" => $list);

        return $this->success('',$result);
    }


    /**
     * 客户查重
     */
    public function reduplicate()
    {

        //如果发送的来源是Selectpage，则转发到Selectpage
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->with([
                'createUser' => function ($user) {
                    $user->field('id,username,nickname');
                },
                'ownerUser' => function ($user) {
                    $user->field('id,username,nickname');
                },
            ])
            ->field('id,name,deal_status,create_time,mobile,create_user_id,owner_user_id,telephone,update_time,follow_time,tags')
            ->order($sort, $order)
            ->limit($offset, $limit)->fetchSql(false)
            ->select();

        $pattern = '/(\d{3})(\d{4})(\d{4})/i';
        $replacement = '$1****$3';
        //为了安全隐藏手机和电话
        foreach ($list as $row) {
            $row->mobile = $row->mobile ? preg_replace($pattern, $replacement, $row->mobile) : '';
            $row->telephone = $row->telephone ? preg_replace($pattern, $replacement, $row->telephone) : '';
        }
        $this->success('', array("rows" => $list));
    }




    /**
     * selectpage
     * @ApiInternal
     * @return \think\response\Json
     */
    public function selectpage()
    {
        $type = $this->request->param('type');
        if ($type == "all") {
            return parent::selectpage(); // TODO: Change the autogenerated stub
        } else {
            return $this->index();
        }

    }
    /**
     * 下一个
     */
    public function next($type='owner'){
        $cachename=$this->auth->id.'cachenextid';
        $prev_id=intval(cache($cachename));
        //自动获取我待跟进的客户
        if ($prev_id){
            $this->model->where('id','>',$prev_id);
        }
        switch ($type){
            case 'all':
                $childrenAdminIds =$this->auth->getChildrenAdminIds(true);
                $this->model->where('owner_user_id','in',$childrenAdminIds);
                break;
            case 'owner';
            default:
                $this->model->where('owner_user_id',$this->auth->id);
        }


        $row= $this->model->where([function ($q){
            return $q->where('next_time','<>','')->where('next_time','<',time())->whereOr('follow_time','');
        }])->fetchSql(false)->order('id asc')->field('id,name')->find();

        if (!$row){
            cache($cachename,null);
            $this->error(__("没有下一个了"));
        }
        cache($cachename,$row->id);
        $this->success("",$row);
    }

    /**
     * 离职回收
     * @ApiParams(name="leave_user_id", type="string", required=true, description="员工id，多个以逗号隔开")
     * @ApiParams(name="owner_user_ids", type="string", required=true, description="接收资料员工id，多个以逗号隔开")
     * @ApiParams(name="type", type="string", required=true, description="1平均分配2随机")
     * @ApiParams(name="updateleave", type="string", required=true, description="1设置员工为离职状态")
     */
    public function recover(){
        //员工离职回收客户资料
        if ($this->request->isPost()) {
            $leave_user_id = $this->request->post("leave_user_id", '');
            $owner_user_ids = $this->request->post("owner_user_ids", '');
            $updateleave = $this->request->post("updateleave", '');

            $type = $this->request->post("type", '1');//分配方式


            if (!$leave_user_id)
                $this->error(__("未选择离职员工"));

            if (!$owner_user_ids)
                $this->error(__("未选择接受资料员工"));

            $this->childrenAdminIds =$this->auth->getChildrenAdminIds($this->auth->id, true);
            //判断转移的员工是不是下属
            if (array_diff_assoc(explode(',',$owner_user_ids), $this->childrenAdminIds)) {
                $this->error(__('只能转移给自己或者下属'));
            }
		
			$leave_user_id_arr=explode(',',$leave_user_id);
            if (array_diff_assoc($leave_user_id_arr, $this->childrenAdminIds)) {
                $this->error(__('只能操作自己的下属客户'));
            }
			
			if(in_array($this->auth->id,$leave_user_id_arr)){
				$this->error(__('不能操作自己'));
			}


            $list = $this->model->where('owner_user_id', 'in', $leave_user_id)->fetchSql(false)->select();


            if (!$list) {
                $this->error(__('该员工下面没有客户资料'));
            }
            $admin_ids= explode(',',$owner_user_ids);//转成数组
            $admin_count=count($admin_ids);//管理员数量
            $i=0;//
            $success_num = 0;//成功数量

            $contacts = model('\app\admin\model\facrm\customer\Contacts');
            foreach ($list as $row) {

                if ($type==2){
                    //随机分配客户
                    $admin_id=$admin_ids[mt_rand(0, $admin_count - 1)];
                }else{
                    //平均分配
                    $admin_id=$admin_ids[$i];
                    $i==($admin_count-1)?$i=0:$i++;
                }


                /* $owner_count = $this->model->where('deal_status', 0)->where('owner_user_id', $admin_id)->count();
                 if ($row->deal_status != 1) {//成交不占用数量
                     //判断是否有客户限制
                     if ($this->addon_config['possess_num'] > 0 && $this->addon_config['possess_num'] <= $owner_count) {
                         $this->error(__("已经超过限制拥有客户数量,限制") . $this->addon_config['possess_num'] . ",成功转移{$success_num}条!");
                     }
                     $owner_count++;
                 }*/
                $row->owner_user_id = $admin_id;
                $row->collect_time = time();
                \app\admin\model\facrm\Operatelog::setTitle(__('离职回收'));
                $row->save();
                //更改联系人
                //联系人负责人也更改
                $contacts->where('customer_id', $row->id)->update(['owner_user_id' => $admin_id]);
                $success_num++;
            }

            if ($updateleave){
                (new Admin())->where('id','in',$leave_user_id)->update(['status'=>'hidden']);
            }

            $this->success(__("成功回收客户资料{$success_num}条"));
        }


        return $this->view->fetch();

    }

    /**
     * 分享删除
     * @param string $ids
     */
    public function sdel($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $row = $this->model->where('id',  $ids)->find();
            //判断客户是不是属于当前用户
            if ($row->owner_user_id==$this->auth->id){
                //删除所有共享
                $row->rw_user_id="";
            }else{
                $rw_user_id=explode(',',$row->rw_user_id);
                $key = array_search($this->auth->id,$rw_user_id);

                if(isset($key)){
                    unset($rw_user_id[$key]);
                }else{
                    $this->error("该客户没有分享给你，无法删除分享");
                }

                $row->rw_user_id=implode(',',$rw_user_id);
            }
            \app\admin\model\facrm\Operatelog::setTitle(__('删除分享'));
            $row->save();
            $this->success("");
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));

    }


    /**
     * 分享客户
     * @param string $ids
     * @ApiParams(name="admin_id", type="string", required=true, description="分享员工id，多个以逗号隔开")
     *
     */
    public function share($ids = NULL)
    {
        $ids = $ids ? $ids : $this->request->post("ids");
        $admin_ids = $this->request->post("admin_id", '');
        if (!$ids)
            $this->error(__("请选择客户"));


        $row = $this->model->where('id',  $ids)->where('owner_user_id', $this->auth->id)->fetchSql(false)->find();

        if (!$row){
            $this->error(__("您不是该客户的负责人不能分享"));
        }

        if ($this->request->isPost()) {

            if (!$admin_ids)
                $this->error(__("未选择要分享的同事"));

            $row->rw_user_id=$admin_ids;
            \app\admin\model\facrm\Operatelog::setTitle(__('分享给：%s', [$admin_ids]));
            $row->save();
            $this->success(__("分享成功"));
        }

        $this->error(__('Parameter %s can not be empty', ''));

    }


    /**
     * 成交[非合同]
     * @param int $ids
     * @param int $money
     */
    public function deal($ids = NULL)
    {
        $ids = $ids ? $ids : $this->request->post("ids");
        if (!$this->addon_config['deal']){
            $this->error(__("手动成交已经关闭，请打开或者添加合同"));
        }

        if (!$ids)
            $this->error(__("请选择客户"));
        $row = $this->model->where('id',  $ids)->fetchSql(false)->find();

        if (!$row)
            $this->error(__("客户信息为空"));

        $auth = new \addons\facrm\library\Auth();
        if (!$auth->checkCustomerAuth($row, $this->auth)) {
            $this->error(__('您没有权限管理该客户'));
        }

        if ($this->request->isPost()) {
            $money= $this->request->post("money", '');
            if (!$money||!is_numeric($money)||$money<=0)
                $this->error(__("请输入正确的金额"));

            $row->purchase_total+=$money;
            $row->purchase_times++;
            $row->deal_time=time();
            $row->deal_status=1;
            \app\admin\model\facrm\Operatelog::setTitle(__('成交￥%s', [$money]));
            $row->save();
            $this->success(__("成功"));
        }

        $this->error(__('Parameter %s can not be empty', ''));

    }



}
